﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
public partial class AdminPortal_Controls_TienTe_TienTe_List : System.Web.UI.UserControl
{
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    
    TblTienTeManager _TblTienTeManager = new TblTienTeManager();
    public int PageSize
    {
        get { return Paging1.PageSize; }
        set { Paging1.PageSize = value; }
    }

    protected int _StartIndex;
    public int StartIndex
    {
        get { return _StartIndex; }
        set { _StartIndex = value; }
    }

    public bool Paging
    {
        get { return Paging1.Visible; }
        set { Paging1.Visible = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Paging1.CurrentPage = 1;
            FunctionExtend.LoadPageSize(ddlPageSize);
            LoadDataToGrid(new TblTienTeEntity());
        }
    }

    private void LoadDataToGrid(TblTienTeEntity _TblTienTeEntity)
    {
        Paging1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
        
        _TblTienTeEntity.LangId = LangID;
        _TblTienTeEntity.SortDirection = "ASC";
        _TblTienTeEntity.StartIndex = (Paging1.CurrentPage - 1) * Paging1.PageSize;
        _TblTienTeEntity.EndIndex = Paging1.CurrentPage * Paging1.PageSize;
        if (Paging1.CurrentPage > 1) _TblTienTeEntity.StartIndex += 1;
        DataTable dt = _TblTienTeManager.be_selectToGridView(_TblTienTeEntity);
        if (dt != null)
        {

            dgvListTiente.DataSource = dt;
            dgvListTiente.DataBind();
            lblTotalRecord.Text = dt.Rows.Count.ToString();
            if (dt.Rows.Count > 0)
            {
                Paging1.TotalItems = Convert.ToInt32(dt.Rows[0]["TotalItems"].ToString());
            }

        }
        if (Paging1.Visible) Paging1.BeginPaging();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AdminPortal/Pages/TienTeAdd.aspx");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgvListTiente.Rows)
        {
            HtmlInputCheckBox chkID = (HtmlInputCheckBox)row.FindControl("chkID");
            if (chkID.Checked)
            {
                try
                {
                    _TblTienTeManager.DeleteById(Convert.ToInt32(chkID.Value));
                }
                catch { }
            }
        }
        Paging1.CurrentPage = 1;
        LoadDataToGrid(new TblTienTeEntity());
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        TblTienTeEntity TblTienTeEntity = new TblTienTeEntity();
        TblTienTeEntity.Condition = txtKeySearch.Text;
        TblTienTeEntity.LangId = LangID;
        Paging1.CurrentPage = 1;
        LoadDataToGrid(TblTienTeEntity);

    }
    protected void dgvListTiente_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView r = (DataRowView)e.Row.DataItem;

        HyperLink lnkCode = (HyperLink)e.Row.FindControl("lnkCode");

        if (r != null && lnkCode != null)
        {
            lnkCode.Text = r[TblTienTeFields.Code.Name].ToString();
            lnkCode.NavigateUrl = "~/AdminPortal/Pages/TienTeEdit.aspx?id=" + r[TblTienTeFields.Id.Name].ToString();
        }
    }
    protected void Paging1_OnPageIndexChanged(object sender, EventArgs e)
    {
        LoadDataToGrid(new TblTienTeEntity());
        Paging1.BeginPaging();
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        Paging1.CurrentPage = 1;
        LoadDataToGrid(new TblTienTeEntity());
    }
}